﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAWSharp.FileFormats.WavFile;
using DAWSharp.DSP.Delay;
using DAWSharp.DSP.Dynamics;
using DAWSharp.DSP.EQ;


namespace Benchmarks
{
    class Program
    {       
        static void Main(string[] args)
        {
            Tests tests = new Tests();
            tests.InitializeTestObjects();
            tests.DelayTest();
            tests.AnalogDelayTest();
            tests.APFTest();
            tests.LPFTest();
            tests.HPFTest();
            tests.BPFTest();
            tests.HighShelfTest();
            tests.LowShelfTest();
            tests.NotchTest();
            tests.PeakTest();
            tests.SaturationTest();
            tests.EightBitTest();
                       
            Console.ReadLine();
        }
    }

    public class Tests
    {
        //Test Objects
        WavFile wav;
        Delay delay;
        AnalogDelay adl;
        Saturation sat;
        EightBit eb;
        EQ lpf;
        EQ apf;
        EQ hs;
        EQ hpf;
        EQ bpf;
        EQ ls;
        EQ n;
        EQ p;

        public void InitializeTestObjects()
        {
            wav = new WavFile();
            delay = new Delay(88200);
            adl = new AnalogDelay(88200);
            sat = new Saturation();
            eb = new EightBit();
            lpf = new LPF(500.0f, 1.0f, 44100.0f);
            bpf = new BPF(500.0f, 1.0f, 44100.0f);
            apf = new APF(500.0f, 1.0f, 44100.0f);
            hs = new HighShelf(500.0f, 1.0f, -6.0f, 44100.0f);
            hpf = new HPF(500.0f, 1.0f, 44100.0f);
            ls = new LowShelf(500.0f, 1.0f, -6.0f, 44100.0f);
            n = new Notch(500.0f, 1.0f, 44100.0f);
            p = new Peak(500.0f, 1.0f, -6.0f, 44100.0f);

            wav.OpenWavFile(@"..\..\TEST1SEC.wav");
        }

        public void DelayTest()
        {
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();
            
            for (int i = 0; i < wav.numberOfSamples; i++)
            {
                float sample = wav.GetSample(i);
                sample = sample + delay.Process(sample, 0.3f, 11025);
                wav.SetSample(i, sample);
            }
            
            stopwatch.Stop();
            
            Console.WriteLine("Delay Test Results");
            Console.WriteLine("Time elapsed (s): {0}", stopwatch.Elapsed.TotalSeconds);
            Console.WriteLine("Time elapsed (ms): {0}", stopwatch.Elapsed.TotalMilliseconds);
            Console.WriteLine("Time elapsed (ns): {0}", stopwatch.Elapsed.TotalMilliseconds * 1000000);
            Console.WriteLine(Environment.NewLine);
        }

        public void AnalogDelayTest()
        {
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 0; i < wav.numberOfSamples; i++)
            {
                float sample = wav.GetSample(i);
                sample = sample + adl.Process(sample, 0.3f, 11025);
                wav.SetSample(i, sample);
            }

            stopwatch.Stop();

            Console.WriteLine("Analog Delay Test Results");
            Console.WriteLine("Time elapsed (s): {0}", stopwatch.Elapsed.TotalSeconds);
            Console.WriteLine("Time elapsed (ms): {0}", stopwatch.Elapsed.TotalMilliseconds);
            Console.WriteLine("Time elapsed (ns): {0}", stopwatch.Elapsed.TotalMilliseconds * 1000000);
            Console.WriteLine(Environment.NewLine);
        }

        public void SaturationTest()
        {
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 0; i < wav.numberOfSamples; i++)
            {
                float sample = wav.GetSample(i);
                sample = sat.Process(sample, 0.3f);
                wav.SetSample(i, sample);
            }

            stopwatch.Stop();

            Console.WriteLine("Saturation Test Results");
            Console.WriteLine("Time elapsed (s): {0}", stopwatch.Elapsed.TotalSeconds);
            Console.WriteLine("Time elapsed (ms): {0}", stopwatch.Elapsed.TotalMilliseconds);
            Console.WriteLine("Time elapsed (ns): {0}", stopwatch.Elapsed.TotalMilliseconds * 1000000);
            Console.WriteLine(Environment.NewLine);
        }

        public void EightBitTest()
        {
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 0; i < wav.numberOfSamples; i++)
            {
                float sample = wav.GetSample(i);
                sample = eb.Process(sample);
                wav.SetSample(i, sample);
            }

            stopwatch.Stop();

            Console.WriteLine("Eight Bit Test Results");
            Console.WriteLine("Time elapsed (s): {0}", stopwatch.Elapsed.TotalSeconds);
            Console.WriteLine("Time elapsed (ms): {0}", stopwatch.Elapsed.TotalMilliseconds);
            Console.WriteLine("Time elapsed (ns): {0}", stopwatch.Elapsed.TotalMilliseconds * 1000000);
            Console.WriteLine(Environment.NewLine);
        }

        public void LPFTest()
        {
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 0; i < wav.numberOfSamples; i++)
            {
                float sample = wav.GetSample(i);
                sample = lpf.Process(sample);
                wav.SetSample(i, sample);
            }

            stopwatch.Stop();

            Console.WriteLine("LPF Test Results");
            Console.WriteLine("Time elapsed (s): {0}", stopwatch.Elapsed.TotalSeconds);
            Console.WriteLine("Time elapsed (ms): {0}", stopwatch.Elapsed.TotalMilliseconds);
            Console.WriteLine("Time elapsed (ns): {0}", stopwatch.Elapsed.TotalMilliseconds * 1000000);
            Console.WriteLine(Environment.NewLine);
        }

        public void APFTest()
        {
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 0; i < wav.numberOfSamples; i++)
            {
                float sample = wav.GetSample(i);
                sample = apf.Process(sample);
                wav.SetSample(i, sample);
            }

            stopwatch.Stop();

            Console.WriteLine("APF Test Results");
            Console.WriteLine("Time elapsed (s): {0}", stopwatch.Elapsed.TotalSeconds);
            Console.WriteLine("Time elapsed (ms): {0}", stopwatch.Elapsed.TotalMilliseconds);
            Console.WriteLine("Time elapsed (ns): {0}", stopwatch.Elapsed.TotalMilliseconds * 1000000);
            Console.WriteLine(Environment.NewLine);
        }

        public void BPFTest()
        {
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 0; i < wav.numberOfSamples; i++)
            {
                float sample = wav.GetSample(i);
                sample = bpf.Process(sample);
                wav.SetSample(i, sample);
            }

            stopwatch.Stop();

            Console.WriteLine("BPF Test Results");
            Console.WriteLine("Time elapsed (s): {0}", stopwatch.Elapsed.TotalSeconds);
            Console.WriteLine("Time elapsed (ms): {0}", stopwatch.Elapsed.TotalMilliseconds);
            Console.WriteLine("Time elapsed (ns): {0}", stopwatch.Elapsed.TotalMilliseconds * 1000000);
            Console.WriteLine(Environment.NewLine);
        }

        public void HighShelfTest()
        {
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 0; i < wav.numberOfSamples; i++)
            {
                float sample = wav.GetSample(i);
                sample = hs.Process(sample);
                wav.SetSample(i, sample);
            }

            stopwatch.Stop();

            Console.WriteLine("High Shelf Test Results");
            Console.WriteLine("Time elapsed (s): {0}", stopwatch.Elapsed.TotalSeconds);
            Console.WriteLine("Time elapsed (ms): {0}", stopwatch.Elapsed.TotalMilliseconds);
            Console.WriteLine("Time elapsed (ns): {0}", stopwatch.Elapsed.TotalMilliseconds * 1000000);
            Console.WriteLine(Environment.NewLine);
        }

        public void HPFTest()
        {
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 0; i < wav.numberOfSamples; i++)
            {
                float sample = wav.GetSample(i);
                sample = hpf.Process(sample);
                wav.SetSample(i, sample);
            }

            stopwatch.Stop();

            Console.WriteLine("HPF Test Results");
            Console.WriteLine("Time elapsed (s): {0}", stopwatch.Elapsed.TotalSeconds);
            Console.WriteLine("Time elapsed (ms): {0}", stopwatch.Elapsed.TotalMilliseconds);
            Console.WriteLine("Time elapsed (ns): {0}", stopwatch.Elapsed.TotalMilliseconds * 1000000);
            Console.WriteLine(Environment.NewLine);
        }

        public void LowShelfTest()
        {
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 0; i < wav.numberOfSamples; i++)
            {
                float sample = wav.GetSample(i);
                sample = ls.Process(sample);
                wav.SetSample(i, sample);
            }

            stopwatch.Stop();

            Console.WriteLine("Low Shelf Test Results");
            Console.WriteLine("Time elapsed (s): {0}", stopwatch.Elapsed.TotalSeconds);
            Console.WriteLine("Time elapsed (ms): {0}", stopwatch.Elapsed.TotalMilliseconds);
            Console.WriteLine("Time elapsed (ns): {0}", stopwatch.Elapsed.TotalMilliseconds * 1000000);
            Console.WriteLine(Environment.NewLine);
        }

        public void NotchTest()
        {
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 0; i < wav.numberOfSamples; i++)
            {
                float sample = wav.GetSample(i);
                sample = n.Process(sample);
                wav.SetSample(i, sample);
            }

            stopwatch.Stop();

            Console.WriteLine("Notch Test Results");
            Console.WriteLine("Time elapsed (s): {0}", stopwatch.Elapsed.TotalSeconds);
            Console.WriteLine("Time elapsed (ms): {0}", stopwatch.Elapsed.TotalMilliseconds);
            Console.WriteLine("Time elapsed (ns): {0}", stopwatch.Elapsed.TotalMilliseconds * 1000000);
            Console.WriteLine(Environment.NewLine);
        }

        public void PeakTest()
        {
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 0; i < wav.numberOfSamples; i++)
            {
                float sample = wav.GetSample(i);
                sample = p.Process(sample);
                wav.SetSample(i, sample);
            }

            stopwatch.Stop();

            Console.WriteLine("Peak Test Results");
            Console.WriteLine("Time elapsed (s): {0}", stopwatch.Elapsed.TotalSeconds);
            Console.WriteLine("Time elapsed (ms): {0}", stopwatch.Elapsed.TotalMilliseconds);
            Console.WriteLine("Time elapsed (ns): {0}", stopwatch.Elapsed.TotalMilliseconds * 1000000);
            Console.WriteLine(Environment.NewLine);
        }
    }
}
